package com.baiyun.Ui;

import com.baiyun.util.CodeUtil;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class LoginJFrame extends JFrame implements MouseListener {

    private String sysCode;

    public LoginJFrame() {
        //初始化JFrame界面
        initJFrame();

        //将界面图片初始化
        initImage();

        //点X后让界面消失
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        //保证界面不消失
        this.setVisible(true);
    }


    private JTextField jTextFieldUsername;
    private static String username;
    public static String getUsername(){
        return username;
    }
    private JTextField jTextFieldPassword;
    private static String password;
    public static String getPassword(){
        return password;
    }

    private JTextField checkNum;

    private JButton Login;
    private JButton register;
    private JLabel rightCode;

    private void initImage() {
        this.getContentPane().removeAll();
        //创建JLabel添加用户名文字
        JLabel jLabelName = new JLabel(new ImageIcon("image/login/用户名.png"));
        jLabelName.setBounds(116, 135, 51, 19);
        this.getContentPane().add(jLabelName);

        //创建用户名的文本输入框
        jTextFieldUsername = new JTextField();
        jTextFieldUsername.setBounds(195, 134, 200, 30);
        this.getContentPane().add(jTextFieldUsername);

        //添加密码文字
        JLabel jLabelPassword = new JLabel(new ImageIcon("image/login/密码.png"));
        jLabelPassword.setBounds(130, 195, 35, 18);
        this.getContentPane().add(jLabelPassword);

        //创建密码输入框
        jTextFieldPassword = new JTextField();
        jTextFieldPassword.setBounds(195, 195, 200, 30);
        this.getContentPane().add(jTextFieldPassword);

        //创建验证码文字
        JLabel jLabelCheckNum = new JLabel(new ImageIcon("image/login/验证码.png"));
        jLabelCheckNum.setBounds(133, 256, 50, 30);
        this.getContentPane().add(jLabelCheckNum);

        //创建验证码输入框
        checkNum = new JTextField();
        checkNum.setBounds(195, 256, 100, 30);
        this.getContentPane().add(checkNum);


        sysCode = CodeUtil.getCode();
        rightCode = new JLabel();
        //设置内容
        rightCode.setText(sysCode);
        //位置和宽高
        rightCode.setBounds(300, 256, 50, 30);
        //绑定事件
        rightCode.addMouseListener(this);
        //添加到界面
        this.getContentPane().add(rightCode);

        //创建登录的按钮
        Login = new JButton();
        Login.setIcon(new ImageIcon("image/login/登录按钮.png"));
        Login.setBounds(123, 310, 128, 47);
        //去除按钮的默认边框
        Login.setBorderPainted(false);
        //去除按钮的默认背景
        Login.setContentAreaFilled(false);
        //绑定事件
        Login.addMouseListener(this);
        this.getContentPane().add(Login);

        //创建注册按钮
        register = new JButton();
        register.setBounds(256, 310, 128, 47);
        register.setIcon(new ImageIcon("image/login/注册按钮.png"));
        //去除按钮的默认边框
        register.setBorderPainted(false);
        //去除按钮的默认背景
        register.setContentAreaFilled(false);
        register.addMouseListener(this);
        this.getContentPane().add(register);

        //添加背景
        JLabel jLabelBg = new JLabel(new ImageIcon("image/login/background.png"));
        jLabelBg.setBounds(0, 0, 470, 390);
        this.getContentPane().add(jLabelBg);

        this.getContentPane().repaint();
    }


    private void initJFrame() {
        //设置长宽高
        this.setSize(488, 430);
        //设置标题
        this.setTitle("拼图 登录");
        //将界面置顶
        this.setAlwaysOnTop(true);
        //界面居中
        this.setLocationRelativeTo(null);
        //取消隐藏容器
        this.setLayout(null);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object source = e.getSource();
        if (source == Login) {
            System.out.println("点击了登录");
            username = jTextFieldUsername.getText();
            password = jTextFieldPassword.getText();
            if (username.isEmpty() || password.isEmpty()){
                return;
            }
            if (!checkUsername()) {
                showJDialog("账号名或密码错误");
                return;
            }
            String code = checkNum.getText();
            if (!(code.equalsIgnoreCase(sysCode))) {
                showJDialog("验证码错误");
                return;
            }

            new GameJFrame();
        } else if (source == register) {
            System.out.println("点击了注册");
            this.dispose();
            new RegisterJFrame();
        } else if (source == rightCode) {
            System.out.println("点击了验证码");
            initImage();
        }
    }

    @Override
    public void mousePressed(MouseEvent e){
        Object source = e.getSource();
        if (source == Login){
            Login.setIcon(new ImageIcon("image/login/登录按下.png"));
        } else if (source == register) {
            register.setIcon(new ImageIcon("image/login/注册按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object source = e.getSource();
        if (source == Login) {
            Login.setIcon(new ImageIcon("image/login/登录按钮.png"));
        } else if (source == register) {
            register.setIcon(new ImageIcon("image/login/注册按钮.png"));
        }
    }

    /**
     * 从userInfo中获取用户信息并校验
     * @return
     */
    private boolean checkUsername() {
        File file = new File("userInfo.txt"); // 用户数据存储在 userInfo.txt 中
        //先判断文件中知否有信息且文件是否存在
        if (!file.exists()) {
            showJDialog("用户数据文件不存在！");
            return false;
        }
        //IO流获取逐行获取文件信息
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = reader.readLine()) != null) {
                //每行数据按照 = 分割为用户名和密码
                String[] userData = line.split("=");
                if (userData.length == 2) {
                    String fileUsername = userData[0];
                    String filePassword = userData[1];
                    //检查是否匹配输入的用户名和密码
                    if (username.equals(fileUsername) && password.equals(filePassword)) {
                        reader.close();
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            showJDialog("文件读取异常！");
        }

        return false; //若未找到匹配的用户名和密码，则返回 false
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }

    //要展示用户名或密码错误
    public void showJDialog(String content) {
        //创建一个弹框对象
        JDialog jDialog = new JDialog();
        //给弹框设置大小
        jDialog.setSize(200, 150);
        //让弹框置顶
        jDialog.setAlwaysOnTop(true);
        //让弹框居中
        jDialog.setLocationRelativeTo(null);
        //弹框不关闭永远无法操作下面的界面
        jDialog.setModal(true);

        //创建JLabel对象管理文字并添加到弹框当中
        JLabel warning = new JLabel(content);
        warning.setBounds(0, 0, 200, 150);
        jDialog.getContentPane().add(warning);

        //让弹框展示出来
        jDialog.setVisible(true);
    }
}
